package com.pan.creational.proto.entity;

import java.io.*;
import java.util.Arrays;

/**
 * 要克隆 需要实现 Cloneable 接口
 *
 * 要序列胡 需要实现 Serializable 接口
 *
 *
 */
public class MyReport implements Cloneable, Serializable {
    int id;
    Person author;
    String content;

    /**
     * 浅拷贝
     * @return
     */
    @Override
    public MyReport clone(){
        MyReport report = new MyReport(
                this.id,
                this.author,
                this.content
        );
        return report;
    }

    /**
     * 深拷贝
     * @return
     */
    public MyReport deepClone() throws Exception {

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(this);
        objectOutputStream.flush();

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);

        objectInputStream.close();
        objectOutputStream.close();
        return (MyReport) objectInputStream.readObject();
    }

    public MyReport(int id, Person author, String content) {
        this.id = id;
        this.author = author;
        this.content = content;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Person getAuthor() {
        return author;
    }

    public void setAuthor(Person author) {
        this.author = author;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }


    public MyReport() {
    }

    @Override
    public String toString() {
        return "MyReport{" +
                "id=" + id +
                ", author=" + author.toString() +
                ", content='" + content + '\'' +
                '}';
    }
}
